package com.firebird.app.kotlin.stock.trend.activity

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.view.View
import android.webkit.WebView
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.firebird.app.kotlin.stock.trend.R
import com.firebird.app.kotlin.stock.trend.service.StockService
import java.util.*

/**
 * App启动时的等待窗口，处理进入home页时需要预先加载的内容
 * @author ljk
 * @date   2019年10月5日
 */
class BeginActivity : AppCompatActivity() {
    private val PREFERENCES_NAME = "app_setting"
    private var isFirstInstall = false
    private var preferences: SharedPreferences? = null
    private var timer: Timer? = null
    private var but_skip: Button? = null
    private var wv_begin: WebView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.setContentView(R.layout.activity_begin)
        but_skip = findViewById<View>(R.id.but_skip) as Button
        wv_begin = findViewById<View>(R.id.wv_begin) as WebView
        val webSettings = wv_begin!!.settings
        webSettings.javaScriptEnabled = false //设置WebView属性，能够执行Javascript脚本
        webSettings.allowFileAccess = true //设置可以访问文件
        webSettings.builtInZoomControls = false //设置支持缩放
        wv_begin!!.loadUrl(resources.getString(R.string.url_begin_ad))
        preferences = getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE)
        isFirstInstall = preferences!!.getBoolean("isFirstInstall", true)
        //这里判断是不是初次安装，初次安装进入引导页面，不是是初次安装就直接进入home页
        if (isFirstInstall) {
            val intent = Intent(this, WelcomeActivity::class.java)
            this.startActivity(intent)
            //更新安装状态值，设为false
            val editor = preferences!!.edit()
            editor.putBoolean("isFirstInstall", false)
            editor.apply()
            finish()
        } else {
            timer = Timer()
            timer!!.schedule(object : TimerTask() {
                override fun run() {
                    val intent = Intent(this@BeginActivity, HomeActivity::class.java)
                    this@BeginActivity.startActivity(intent)
                    finish()
                }
            }, 1 * 1000.toLong())
        }
        //启动后台服务控件
        //val intent = Intent(this, StockService::class.java)
        //startService(intent)
    }

    override fun onResume() {
        super.onResume()
        //System.out.println("执行onResume");
//跳过广告按钮监听器
        but_skip!!.setOnClickListener {
            if (timer != null) {
                timer!!.cancel()
            }
            val intent = Intent(this@BeginActivity, HomeActivity::class.java)
            this@BeginActivity.startActivity(intent)
            finish()
        }
    }

    override fun onStart() {
        super.onStart()
        //System.out.println("执行onStart");
    }

    override fun onPause() {
        super.onPause()
        //System.out.println("执行onPause");
    }

    override fun onStop() {
        super.onStop()
        //System.out.println("执行onStop");
    }

    override fun onDestroy() {
        super.onDestroy()
        //System.out.println("执行onDestroy");
    }
}